package PATBrushQuestions;

import com.sun.source.tree.Tree;

import java.util.*;

public class Unit_1003 {

}

class unit_1003{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        for (int i = 0; i < k; i++) {
            boolean fg = false;
            String Str = sc.next();
            //必须包含PAT3种字符
            if (Str.contains("P") && Str.contains("A") && Str.contains("T")) {
                String s = Str;
                s = s.replace("P", "");
                s = s.replace("A", "");
                s = s.replace("T", "");
                //必须不包含除了PAT之外的字母
                if (s.isEmpty()) {
                    int p = Str.indexOf("P");
                    int t = Str.indexOf("T");
                    int len = Str.length();
                    /*
                    (m个A) P （n个A） T （z个A）
                     需要满足 m * n = Z
                     */
                    int m = p;
                    int n = t - p - 1;
                    int z = len - t - 1;
                    if (m * n == z) {
                        System.out.println("YES");
                        fg = true;
                    }
                }
            }
            if (!fg) System.out.println("NO");
        }
    }
}


class unit_1004{
    public static void main(String[] args) {
        List<Student> stds = new ArrayList<Student>();
        Scanner input = new Scanner(System.in);
        int num = Integer.parseInt(input.nextLine());
        while (num > 0) {
            String str = input.nextLine();
            //System.out.println(str);
            String[] temp = str.split(" ");
            Student std = new Student();
            std.name = temp[0];
            std.stuId = temp[1];
            std.score = Integer.parseInt(temp[2]);
            stds.add(std);
            num--;
        }
        //Collections的sort（）方法可以对List进行排序
        Collections.sort(stds);
        System.out.println(stds.get(0).name + " " + stds.get(0).stuId);
        System.out.println(stds.get(stds.size() - 1).name + " " + stds.get(stds.size() - 1).stuId);
    }

}

class Student implements Comparable<Student> {
    String name;
    String stuId;
    int score;

    @Override
    public String toString() {
        return "Student [name=" + name + ", stuId=" + stuId + ", score=" + score + "]";
    }

    @Override
    public int compareTo(Student o) {
        // TODO Auto-generated method stub
        return -(score - o.score);
    }

}

